﻿
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * 
 * C O P Y R I G H T    N O T I C E
 * 
 * Copyright (c) 2000-2010 Acidaes Solutions Pvt. Ltd. All Rights Reserved.
 * 
 * This code is a part of the source code used by Acidaes Solution Private Limited for its product "BusinessNext".
 * You may not, directly or indirectly, use, disclose, distribute, print or copy this code or any part of it.
 * 
 * This code contains confidential information and/or may contain information protected by intellectual property 
 * rights or other rights. This code shall/may not be construed to constitute any commitment from 
 * Acidaes Solutions Private Limited or its subsidiaries or affiliates except when expressly agreed to in a 
 * separate written agreement.
 * 
 * AUTHORS:                           DATE:
 * 
 * REVISION: $Revision: 5420 $
 * LAST MODIFIED BY: &Author$ on $Date: 2007-06-16 12:01:05 +0530 (Sat, 16 Jun 2007) $  
 * 
 * R E V I S I O N  H I S T O R Y:
 * 
 * DATE      CHANGED BY               COMMENT     
 * 
 *  
 * C O D E  R E V I E W  H I S T O R Y: 
 * 
 * DATE      REVIEWED BY              COMMENT     
 *  
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Caching;
using System.Data.Caching.AdminApi;
using System.Data.Caching.Commands;
using System.Configuration;
using System.Collections.Specialized;
using System.Configuration.Provider;


namespace Acidaes.Caching.Velocity
{
    public class VelocityCacheProvider : CacheProvider
    {
        private string _applicationName;

        public override string ApplicationName
        {
            get { return _applicationName; }
            set { _applicationName = value; }
        }



        CacheFactory cacheFactory = new CacheFactory();
        Cache cache;


        public VelocityCacheProvider() 
        {
            string cacheName = ConfigurationSettings.AppSettings["cacheName"];
            cache = GetCache(cacheName);
         
        }


        public override object Add(string key, object value)
        {
            return cache.Add(key, value);
        }
        

        public override Object Get(string key)
        {
            return cache.Get(key);
        }

        public override Object Get(string key,ref CacheMemberVersion version)
        {
            return cache.Get(key);
        }

        public override Object Put(string key, object value)
        {
            return cache.Put(key, value);
        }
        public override void Remove(string key)
        {
            cache.Remove(key);
        }

        public override void RemoveAll()
        {
            throw new NotImplementedException();
        }
       
        public Cache GetCache(string strCacheName)
        {

            return cacheFactory.GetCache(strCacheName);
        }

       

      
        
        /// <summary>
        /// Gets an object from the specified region by using the specified key. 
        /// </summary>
        /// <param name="region"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public Object Get(string region, string key)
        {
            return cache.Get(region, key);
        }

        /// <summary>
        /// Gets an object from the specified region by using the specified key. You may also provide the version to obtain the specific version of a key, if that version is still the most current in the region. 
        /// </summary>
        /// <param name="region"></param>
        /// <param name="key"></param>
        /// <param name="version"></param>
        /// <returns></returns>
        public Object Get(string region, string key, ref CacheItemVersion version)
        {
            return cache.Get(region, key, ref version);
        }

        /// <summary>
        /// Adds an object to the cache. Provides the option to save the object to a region. 
        /// </summary>
        /// <param name="region">The name of the region to which to save the object. Use DefaultRegion if the object is not stored in a region.</param>
        /// <param name="key">A unique value that is used to store and retrieve the object from the region or cache.</param>
        /// <param name="value">The object saved to the cache cluster.</param>
        /// <returns>A CacheItemVersion object that represents the version of the object saved to the cache under the key value. </returns>
        public CacheItemVersion Add(string region, string key, Object value)
        {
            return cache.Add(region, key, value);
        }

        public CacheItemVersion Add(string region, string key, Object value, Tag[] tags)
        {
            return cache.Add(region, key, value, tags);
        }

        /// <summary>
        /// Adds an object to the cache. Provides the option to save the object to a region and specify an object time-out that overrides the default setting from the cache. 
        /// </summary>
        /// <param name="region"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="timeOut"></param>
        /// <returns></returns>
        public override object Add(string region, string key, object value, TimeSpan timeOut)
        {
            return cache.Add(region, key, value, timeOut);
        }
        /// <summary>
        /// Adds an object to the cache. This method provides the option to save the object to a region and enables associating tags with the cached object and specifying how long the object should remain in the cache. 
        /// </summary>
        /// <param name="region"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="tags">An array of Tag objects to associate with the cached object inside the region. If the region name is not provided, all tags are ignored.</param>
        /// <param name="timeOut"></param>
        /// <returns></returns>
        public CacheItemVersion Add(string region, string key, Object value, Tag[] tags, TimeSpan timeOut)
        {
            return cache.Add(region, key, value, tags, timeOut);
        }

       


        public override void Initialize(string name, NameValueCollection config)
        {
            // Verify that config isn't null
            if (config == null)
                throw new ArgumentNullException("config");

            // Assign the provider a default name if it doesn't have one
            if (String.IsNullOrEmpty(name))
                name = "VelocityCacheProvider";

            // Add a default "description" attribute to config if the
            // attribute doesn't exist or is empty
            if (string.IsNullOrEmpty(config["description"]))
            {
                config.Remove("description");
                config.Add("description",
                    "Velocity Cache provider");
            }

            // Call the base class's Initialize method
            base.Initialize(name, config);

            // Initialize _applicationName
            _applicationName = config["applicationName"];

            if (string.IsNullOrEmpty(_applicationName))
                _applicationName = "/";

            config.Remove("applicationName");

            // Throw an exception if unrecognized attributes remain
            if (config.Count > 0)
            {
                string attr = config.GetKey(0);
                if (!String.IsNullOrEmpty(attr))
                    throw new ProviderException
                        ("Unrecognized attribute: " + attr);
            }
        }




        

       
        
    }
}
